/*
* Copyright 2005-2008 Pulse Technologies. Tutti i diritti riservati.
*/
package org.obsidianrad.server.web;

import java.io.File;
import java.util.Properties;

import javax.servlet.ServletContext;

import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.DefaultHandler;
import org.mortbay.jetty.handler.HandlerCollection;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.security.SslSocketConnector;
import org.mortbay.jetty.servlet.FilterHolder;
import org.mortbay.jetty.servlet.ServletHolder;
import org.mortbay.jetty.webapp.WebAppContext;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.DispatcherServlet;

/**
 * 
 * @author eliosh
 * @version $Rev: 17 $
 * @lastautorcommit $Author: eliosh $
 * @lastdatecommit $Date: 2010-01-04 23:13:34 +0100(lun, 04 gen 2010) $
 */
public class JettyWebServer extends WebServer {
	private Server server;
	private WebAppContext webappcontext;
	private Properties _props;

	// ####### COSTRUTTORI - INIZIO #########
	public JettyWebServer(String _basePath, String _contextPath, Properties props) {
		super(_basePath, _contextPath, props);
	}

	public JettyWebServer(File _basePath, String _contextPath, Properties props) {
		super(_basePath, _contextPath, props);
	}

	
	public JettyWebServer(File path, String path2) {
		super(path, path2);
	}

	public JettyWebServer(String path, String path2) {
		super(path, path2);
	}
	// ####### COSTRUTTORI - FINE #########

	
	
	@Override
	public ServletContext getServletContext() {
		return (webappcontext!=null)?webappcontext.getServletContext():null;
	}

	@Override
	public void init(Properties props) {
		_props = props;
		initContext();
	}

	@Override
	public void reloadContext() {
		try {
			webappcontext.stop();
			Thread.sleep(2000);
			webappcontext.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void startServer() throws Exception {
		setServlets(webappcontext);
		setFilters(webappcontext);

		try {
			server.start();
			server.join();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void initContext(){
		server = new Server();
		Connector connector = new SelectChannelConnector();
		connector.setPort(8080);

		Connector[] connectorList = null;
		Connector secureConnector = this.getSecureConnector();
		if(secureConnector!=null){
			connectorList = new Connector[]{connector, secureConnector};
		}else{
			connectorList = new Connector[]{connector};
		}
		server.setConnectors(connectorList);

		String webappPath = this.getBasePath().getAbsolutePath();
		try {
			webappPath = this.getBasePath().getCanonicalPath();
			webappcontext = new WebAppContext();
			webappcontext.setContextPath(this.getContextPath());
			webappcontext.setExtractWAR(false);
			webappcontext.setResourceBase(new File(webappPath).toURI().toURL().toString());
			
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		// setContextLoader();
		HandlerCollection handlers= new HandlerCollection();
		handlers.setHandlers(new Handler[]{webappcontext, new DefaultHandler()});

		server.setHandler(handlers);
	}
	
	private Connector getSecureConnector(){
		if(this.isSecure()){
			SslSocketConnector sslConnector = new SslSocketConnector();
			sslConnector.setPort(8443);
			return sslConnector;
		}else{
			return null;
		}

	}

	@Override
	public void stopServer() throws Exception {
		try{
		webappcontext.stop();
		webappcontext = null;
		server.stop();
		server = null;
		} catch(Throwable t){
			
		}
	}

	/**
	 * Installiamo la servlet Dispatcher per la gestione di tutte le richieste
	 * @param context
	 */
	public void setServlets(WebAppContext context){
/*
		// GWT
		ServletHolder sh = new ServletHolder(new DispatcherServlet());
		sh.setDisplayName("GXT Dispatcher servlet");
		sh.setInitParameter("contextConfigLocation", "classpath:/it/pulsetech/obsidian/backend/conf/dispatcher-conf.xml");
		context.addServlet(sh, "/gwt-rpc/*");
*/	

		// HESSIAN 
		ServletHolder sh = new ServletHolder(new DispatcherServlet());
		sh.setDisplayName("Remoting servlet");
		sh.setInitParameter("contextConfigLocation", "classpath:/it/pulsetech/tsdnobs/server/conf/remoting-conf.xml");
		context.addServlet(sh, "/remoting/*");

/*
		// FLEX
		ServletHolder sh = new ServletHolder(new DispatcherServlet());
		sh.setDisplayName("Remoting servlet");
		sh.setInitParameter("contextConfigLocation", "classpath:/it/pulsetech/tsdnobs/server/conf/spring-flex-conf.xml");
		context.addServlet(sh, "/messagebroker/*");
*/
/*	
		XMLRPC
		ServletHolder sh = new ServletHolder(new DispatcherServlet());
		sh.setDisplayName("Remoting servlet");
		sh.setInitParameter("contextConfigLocation", "classpath:/it/pulsetech/tsdnobs/server/conf/xmlrpc-conf.xml");
		context.addServlet(sh, "/xmlrpc");
*/
	}

	public void setFilters(WebAppContext context){

		FilterHolder fh = new FilterHolder(new DelegatingFilterProxy());
		fh.setName("springSecurityFilterChain"); // is an internal infrastructure bean created by the namespace to handle web security
		context.addFilter(fh, "/*", 0);

	}

	@Override
	public void setBasePath(File basePath) {
		super.setBasePath(basePath);
		try {
			stopServer();
			init(_props);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}